/**
 * Bestpay.com.cn Inc.
 * Copyright (c) 2011-2018 All Rights Reserved.
 */
package com.sgzn.sgpay.util;

import com.sgzn.sgpay.model.response.CurrentPageInfo;

/**
 * 分页计算器
 * @author zhongzhongyong
 * @version Id: PageCalculator.java, v 0.1 2018/5/1 10:33 zhongzhongyong Exp $$
 */
public class PageCalculator {

    /**
     * 计算分页信息
     * @param totalCount 总行数【必传，调用方计算】
     * @param pageSize 页大小【必传，建议为10】
     * @param currentPage 当前页码【必传，最小为1】
     * @return
     */
    public static CurrentPageInfo getPageInfo(int totalCount,int pageSize,int currentPage){
        CurrentPageInfo target = new CurrentPageInfo();
        int totalPage = totalCount%pageSize == 0 ? (totalCount/pageSize):(totalCount/pageSize)+1;//总页数
        int startIndex = 0;//开始行数
        int endIndex = 0;//结束行数
        if(currentPage <= totalPage){
            endIndex = currentPage * pageSize;
        }else{
            //超过总页数，默认查询最后一页
            currentPage = totalPage;
            endIndex = totalCount;
        }
        startIndex = totalCount == 0 ? 0 : (currentPage - 1)*pageSize;
        target.setTotalCount(totalCount);
        target.setCurrentPage(currentPage);
        target.setTotalPage(totalPage);
        target.setPageSize(pageSize);
        target.setStartIndex(startIndex);
        target.setEndIndex(endIndex);
        return target;
    }
}
